﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class StorageDAL
    {
        private ManageStoreDataContext db;

        public StorageDAL()
        {
            db = new ManageStoreDataContext();
        }

        public List<Storage> StorageGetAll()
        {
            var St = from s in db.Storages
                     orderby s.StoregeName
                     select s;

            return St.ToList<Storage>();
        }

        public Storage StorageGetByID(int storageID)
        {
            Storage St = db.Storages.Single<Storage>(s => s.StorageID == storageID);

            return St;
        }

        public bool StorageInsert(Storage st)
        {
            try
            {
                db.Storages.InsertOnSubmit(st);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool StorageUpdate(Storage st)
        {
            Storage St = db.Storages.Single<Storage>(s => s.StorageID == st.StorageID);

            if (St == null)
                return false;

            St.StoregeName = st.StoregeName;
            St.EmployeeID = st.EmployeeID;
            St.Address = st.Address;
            St.PhoneNumber = st.PhoneNumber;

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool StorageDeleteID(int storageID)
        {
            Storage St = db.Storages.Single<Storage>(s => s.StorageID == storageID);

            if (St == null)
                return false;
            db.Storages.DeleteOnSubmit(St);

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool StorageCheckDuplicateName(string StorageName)
        {
            var S = from s in db.Storages
                    select s;
            if (S.ToList<Storage>().Count == 0)
                return true;
            else
            {
                Storage St = db.Storages.Single<Storage>(s => s.StoregeName.Equals(StorageName));

                if (St == null)
                    return true;
                return false;
            }
        }
    }
}
